package NG::Sys::Loop::Run;
use strict;
use warnings;
use utf8;

use base 'NG::Sys::Loop';

sub on_before_loop {
	my ($self) = @_;
	binmode STDOUT,":utf8";
	binmode STDIN,":utf8";
	binmode STDERR,":utf8";
	return 1;
}
sub arg {
	my ($self) = @_;
	if($self->{_parsed_args}){
		return $self->{_parsed_args};
	}
	my %result;
	my $last_key;
	for(my $i = 0; $i < @ARGV; $i++){
		my $val = $ARGV[$i];
		if($val=~s/^-{1,2}//){
			if($last_key){
				$result{$last_key} = 1;
			}
			$last_key = $val;
		}elsif($last_key){
			unless(utf8::is_utf8 $val){
				utf8::decode $val;
			}
			if(exists $result{$last_key}){
				if(ref $result{$last_key}){
					push @{$result{$last_key}}, $val;
				}else{
					$result{$last_key} = [$result{$last_key},$val];
				}
			}else{
				$result{$last_key} = $val;
			}
			$last_key = undef;
		}
	}
	if($last_key){
		$result{$last_key} = 1;
	}
	return $self->{_parsed_args} = \%result;
}

1;
